<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>购买记录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .nav-links {
            display: flex;
            gap: 20px;
        }
        .nav-links a {
            text-decoration: none;
            color: #333;
            padding: 8px 16px;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .nav-links a:hover {
            background-color: #f0f0f0;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #4A90E2;
            color: white;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
            transition: background-color 0.3s;
        }
        .btn-add {
            background-color: #4CAF50;
        }
        .btn-delete {
            background-color: #f44336;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-content {
            background-color: white;
            margin: 15% auto;
            padding: 20px;
            width: 50%;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group select, .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .price-display {
            color: #e53935;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>购买记录</h1>
        <div class="nav-links">
            <a href="customer?action=list">顾客管理</a>
            <a href="guitar?action=list">吉他管理</a>
            <a href="purchase?action=list">购买记录</a>
        </div>
        <button class="btn btn-add" onclick="showAddModal()">添加记录</button>
    </div>

    <table>
        <thead>
        <tr>
            <th>顾客</th>
            <th>吉他品牌</th>
            <th>价格</th>
            <th>购买时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${records}" var="record">
            <tr>
                <td>${record.customerName}</td>
                <td>${record.guitarBrand}</td>
                <td class="price-display">￥${record.price}</td>
                <td>
                    <fmt:formatDate value="${record.purchaseTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
                </td>
                <td>
                    <button class="btn btn-delete"
                            onclick="deleteRecord(${record.customerId}, ${record.guitarId})">
                        删除
                    </button>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>

<!-- 添加购买记录的模态框 -->
<div id="purchaseModal" class="modal">
    <div class="modal-content">
        <h2>添加购买记录</h2>
        <form id="purchaseForm" action="purchase" method="post">
            <input type="hidden" name="action" value="add">
            <div class="form-group">
                <label>顾客：</label>
                <select name="customerId" required>
                    <c:forEach items="${customers}" var="customer">
                        <option value="${customer.id}">${customer.username}</option>
                    </c:forEach>
                </select>
            </div>
            <div class="form-group">
                <label>吉他：</label>
                <select name="guitarId" id="guitarSelect" onchange="updatePrice()" required>
                    <c:forEach items="${guitars}" var="guitar">
                        <option value="${guitar.id}" data-price="${guitar.price}">
                                ${guitar.brand} - ${guitar.type}
                        </option>
                    </c:forEach>
                </select>
            </div>
            <div class="form-group">
                <label>价格：</label>
                <input type="number" name="price" id="price" step="0.01" required>
            </div>
            <button type="submit" class="btn btn-add">保存</button>
            <button type="button" class="btn btn-delete" onclick="hideModal()">取消</button>
        </form>
    </div>
</div>

<script>
    function showAddModal() {
        document.getElementById('purchaseModal').style.display = 'block';
        updatePrice(); // 初始化价格
    }

    function hideModal() {
        document.getElementById('purchaseModal').style.display = 'none';
    }

    function deleteRecord(customerId, guitarId) {
        if (confirm('确定要删除这条购买记录吗？')) {
            window.location.href = `purchase?action=delete&customerId=${customerId}&guitarId=${guitarId}`;
        }
    }

    function updatePrice() {
        const select = document.getElementById('guitarSelect');
        const option = select.options[select.selectedIndex];
        const price = option.getAttribute('data-price');
        document.getElementById('price').value = price;
    }
</script>
</body>
</html>